<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm" @submit.native.prevent>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/deal/home/dealBaseHome',query:{id:form.dealId,companyId:form.companyId}}"
                class="link_a"
              >{{ form.dealName }} </router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司名称')">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/company/base/companyBaseHome',query:{companyId:form.companyId}}"
                class="link_a"
              >{{ form.companyName }}</router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" placeholder="请输入标题" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否需要基金决策')" prop="whetherFundDecisionIsRequired">
            <dict-select v-model="form.whetherFundDecisionIsRequired" dictionary="sys_yes_no" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" show-word-limit :maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="deal_process" :business-id="form.dealId" :btn-show="!otherBtnHide" />
    </el-form>
  </div>
</template>

<script>
    import { saveDealWfGeneral, getDealWfGeneral, getByWfNodeId } from '@/api/sass/heyuan/dealVote'
    // import { getDealBase } from '@/api/base/deal/dealBase'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                  actTitle: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  whetherFundDecisionIsRequired: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
                },
                // 遮罩层
                loading: false,
                // 操作按钮默认不展示
                buttonShow: true,
                // 是否隐藏保存按钮
                otherBtnHide: false
              // dealType: undefined
            }
        },
        created() {
            this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
              this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealWfGeneral(data.id).then(response => {
                      // getDealBase(response.data.dealId).then(result => {
                      //   this.dealType = result.data.dealType
                      // })
                      this.form = response.data
                      this.loading = false
                    })
                } else {
                  getByWfNodeId(data.wfNodeId).then(response => {
                    // 根据项目id查询项目信息
                    // getDealBase(response.data.dealId).then(result => {
                    //   this.dealType = result.data.dealType
                    // })
                    this.form = response.data
                    this.form.fileList = data.fileList
                    this.form.actTitle = data.actTitle
                    this.form.whetherFundDecisionIsRequired = 'Y'
                    this.loading = false
                  })
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                dealId: this.$route.query.id,
                companyId: undefined,
                  whetherFundDecisionIsRequired: 'Y',
                actTitle: undefined,
                remark: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            /** 提交按钮 */
            submitForm: function() {
              return new Promise((resolve, reject) => {
                this.$refs['form'].validate(valid => {
                  // 验证数据
                  if (valid) {
                    // 弹出遮罩层
                    // console.log(this.form.whetherFundDecisionIsRequired, this.dealType)
                    // if (this.form.whetherFundDecisionIsRequired === 'Y') {
                    //   if (this.dealType === 'equity') {
                    //     const data = this.form.fileList.filter(file => file.directoryId === '03e52c77d5a94b23af847015bfd5aa27')
                    //     if (!data || data.length <= 0) {
                    //       this.$message.error('请上传基金决策文件')
                    //       return
                    //       // throw new Error('请上传基金决策文件')
                    //     }
                    //   } else if (this.dealType === 'bf') {
                    //     const data = this.form.fileList.filter(file => file.directoryId === '6f88a9ffcd06463c83869514546fbcc3')
                    //     if (!data || data.length <= 0) {
                    //       this.$message.error('请上传基金决策文件')
                    //       return
                    //       // throw new Error('请上传基金决策文件')
                    //     }
                    //   }
                    // }

                    this.loading = true

                    // 保存数据
                    saveDealWfGeneral(this.form).then(response => {
                      if (response.code === 200) {
                        this.msgSuccess(this.$t('保存成功'))
                        this.loading = false
                        resolve(response.data.id)
                      } else {
                        this.msgError(response.msg)
                        this.loading = false
                        reject(response)
                      }
                    }).catch(() => {
                      this.loading = false
                    })
                  } else {
                    reject()
                    this.locationError(document)
                  }
                })
              })
           }

        }

    }
</script>
